home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / i / internet / software / lprsr / lpr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-30  |  8.5 KB  |  404 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <aes.h>
  5. #include <tos.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <time.h>
  9. #include "tcpdef.h"
  10.  
  11. #define SYSBASE ((SYSHDR*)0x4f2L)
  12.  
  13. static BASPAG **oldpd;
  14. long set_pd(void);
  15. long restore_pd(void);
  16.  
  17. int connect(char * host, int tcp_id);
  18. void output(char *);
  19. void output1(int, char*);
  20. void printfile(FILE *fin);
  21. int get_response(void);
  22.  
  23. #define min(a,b) (((a) < (b)) ? (a) : (b))
  24. #define INBUFSIZE 4096
  25. #define TRUE 1
  26. #define FALSE 0
  27.  
  28. #define FILE_BUF_SIZE      4096
  29. #define noDEBUG
  30.  
  31. DESTI desti;
  32. TCPSTAT tstat;
  33.  
  34. char *myid = "@(#)ANS-LPR 1.0 pm/hw";
  35.  
  36. char *term;
  37. int tcp_buff = 1024;
  38. char data[INBUFSIZE];
  39.  
  40. long number,timev;
  41. char o_str[100];
  42. int tcp_ok;
  43. char file_buffer[FILE_BUF_SIZE];
  44. char filename[50];
  45. char printer[50];
  46. char printhost[50];
  47. char hostname[50];
  48. char user[30];
  49. int o_len;
  50. long lendata;
  51. long lensent;
  52. char *term;
  53. int tcp;
  54. char response;
  55. long filelength;
  56. char * file_to_print;
  57. FILE * fin;
  58. static int error;
  59.  
  60. #define LPR_PORT    515
  61.  
  62. main(int argc, char *argv[])
  63. {
  64. char *arg;
  65. long i;
  66.  
  67.  term = (char *)getenv("PRINTER");
  68.  if(!term)
  69.    strcpy(printer,"lp");
  70.  else
  71.    strcpy(printer,term);
  72.  
  73.  term = (char *)getenv("USER");
  74.  if(!term)
  75.    strcpy(user,"user@ST");
  76.  else
  77.    strcpy(user,term);
  78.  
  79.  term = (char *)getenv("HOSTNAME");
  80.  if(!term)
  81.    strcpy(hostname,"someST");
  82.  else
  83.    strcpy(hostname,term);
  84.  strtok(hostname,".");      /* cut off domainname */
  85.  
  86.  term = (char *)getenv("PRINTHOST");
  87.  if(!term)
  88.    printhost[0]=0;
  89.  else
  90.    strcpy(printhost,term);
  91.  
  92.  while (argc > 1 && argv[1][0] == '-')
  93.  {
  94.      i = 1;  /* start on argument just past - sign */
  95.       argc--;
  96.        arg = *++argv;
  97.  
  98.         do switch(arg[i]) 
  99.         {
  100.      case 'S':
  101.        if (arg[2]) /* get print server name */
  102.     {
  103.         strcpy(printhost,arg+2);
  104.         i = strlen(printhost) + 1;
  105.       }
  106.       else if (argc > 1)
  107.       {
  108.         argc--;
  109.         strcpy(printhost,*++argv);
  110.       }
  111.       break;
  112.  
  113.     case 'P':       /* specify printer name */
  114.       if (arg[2])
  115.       {
  116.         strcpy(printer, &arg[2]);
  117.         i = strlen(printer)+ 1;
  118.       }
  119.       else if (argc > 1)
  120.       {
  121.         argc--;
  122.         strcpy(printer, *++argv);
  123.       }
  124.       break;
  125.     }
  126.     while(arg[++i]);
  127.  
  128.   }
  129.  
  130.  error = 0;
  131.  while(argc > 1)
  132.  {
  133.    file_to_print = argv[1];
  134.    fin = fopen(file_to_print, "rb");
  135.    if(fin == NULL) 
  136.    {
  137.       printf("\r\n<\033pError: can't open file '%s'\033q>\r\n", file_to_print);
  138.       error++;
  139.    }
  140.    else
  141.    {
  142.     printf("\r\nTrying print server %s, device %s, file %s ...\n",printhost,printer,file_to_print);
  143.     timev = clock();
  144.     number = (((timev >> 24) & 0xff) + ((timev >> 16) & 0xff))*15 +
  145.           ((timev >> 10) );
  146.   fseek(fin, 0L,SEEK_END);
  147.   filelength = ftell(fin);
  148.   fseek(fin, 0L, 0);
  149.   lensent = 0;    
  150.     if((tcp = connect(printhost,LPR_PORT)) > 0)
  151.     {
  152.     tcp_ok = TRUE;
  153.       printfile(fin); 
  154.       tcp_close(tcp);
  155.   }
  156.   else
  157.   {
  158.     printf("\r\n<\033pError: no response from %s\033q>\r\n",printhost);
  159.     error++;
  160.     tcp_ok = FALSE;
  161.     }
  162.     fclose(fin);
  163.    }
  164.    argv++;
  165.    argc--;
  166.  }
  167.  if(error)
  168.  {
  169.   printf("\r\n<\033pCR\033q>\r\n");
  170.   Cconin();
  171.  }
  172.  return(0);
  173. }
  174. /* open a connection and return tcp handle, 0 if error */
  175.  
  176. int connect(char * host, int port)
  177. {
  178.   int tcp_id, state;
  179.   
  180.   term = (char *)getenv("TCPWND");
  181.   if(term) tcp_buff = atoi(term);
  182.  
  183.   desti.Port = port;
  184.  
  185.   if(GetIPAddr(host,desti.IPAddr))
  186.   {
  187.     unsigned int tmp1,tmp2,tmp3,tmp4;
  188.     
  189.     if(sscanf(host,"%d.%d.%d.%d",&tmp1,&tmp2,&tmp3,&tmp4) != 4)
  190.     {
  191.       printf("\r\n<\033pError: unknown host.\033q>\r\n");
  192.       return 0;
  193.     }
  194.     desti.IPAddr[0] = tmp1;
  195.     desti.IPAddr[1] = tmp2;
  196.     desti.IPAddr[2] = tmp3;
  197.     desti.IPAddr[3] = tmp4;
  198.   }
  199.   
  200.   tcp_id = (unsigned)tcp_open(721+(int)(Random() % 10),&desti,AKTIV,60,(long)tcp_buff);
  201.  
  202.   if(tcp_id == 0)
  203.   {
  204.     printf("\r\n<\033pError: could not open connection.\033q>\r\n");
  205.     return 0;
  206.   }
  207.  
  208.   do
  209.   {
  210.     state = (int)tcp_stat(tcp_id,&tstat);
  211.     if((state > ESTABLISHED) || (state <= CLOSED)) break;
  212.   }
  213.   while(state < ESTABLISHED);
  214.  
  215.   if(state != ESTABLISHED)
  216.   {
  217.     printf("\r\n<\033pError: connection refused.\033q>\r\n");
  218.     return 0;
  219.   }
  220.   return(tcp_id);
  221. }
  222.  
  223. void printfile(FILE *fin)
  224. {
  225.     /* connection is open, start the lprd protocol */
  226.     sprintf(o_str, "\2%s\n",printer);
  227.     output(o_str);
  228.     if(get_response() > 1)
  229.     {
  230.        printf("%s",data);
  231.     }
  232.       if(response == '\1') 
  233.       {
  234.         printf("\r\n<\033pError: printer server didn't accept printer\033q>\r\n");
  235.         tcp_ok = FALSE;
  236.         return;
  237.       }
  238.       else if(response != '\0') 
  239.       {
  240.         tcp_ok = FALSE;
  241.         return;
  242.       }
  243.       sprintf(filename, "fA%03ld%s",(number++) % 999,hostname);
  244.                 /* get file length */
  245.       sprintf(o_str, "\3%ld d%s\n", filelength, filename);
  246. #ifdef DEBUG
  247.       printf("######### file %s %ld bytes \n",filename,filelength);
  248. #endif
  249.       output(o_str);
  250.     if(get_response() > 1)
  251.     {
  252.        printf("%s",data);
  253.     }
  254.       if(response == '\1') 
  255.       {
  256.         printf("\r\n<\033pError: connection messed up, try again\033q>\r\n");
  257.         tcp_ok = FALSE;
  258.         return;
  259.       }
  260.       else if(response == '\2') 
  261.       {
  262.         printf("\r\n<\033pError: server out of storage space\033q>\r\n");
  263.         tcp_ok = FALSE;
  264.         return;
  265.       }
  266.       else if(response != '\0') 
  267.       {
  268.         tcp_ok = FALSE;
  269.         return;
  270.       }
  271.  
  272.       do
  273.       {
  274.       lendata = fread(file_buffer, 1, min((long)FILE_BUF_SIZE,filelength),fin);
  275. #ifdef DEBUG
  276.       printf("read %ld bytes\n",lendata);
  277. #endif
  278.       if(lendata <= 0)
  279.       {
  280.        file_buffer[0] = 0;
  281.        output1(1,file_buffer);
  282.       }
  283.       if(lendata < 0)
  284.       {
  285.         return;
  286.       }
  287.       lensent += lendata;
  288.       printf("\r%ld %%",(lensent * 100) / filelength);
  289.       output1((int)lendata,file_buffer);
  290.       }while(lendata > 0);      
  291.  
  292.     if(get_response() > 1)
  293.     {
  294.        printf("%s",data);
  295.     }
  296.       if(response != '\0') 
  297.       {
  298.         printf("\r\n<\033pError: data file not properly transferred, aborting\033q>\r\n");
  299.         tcp_ok = FALSE;
  300.         return;
  301.       }
  302. #ifdef DEBUG
  303.       printf("file closed\n");
  304. #endif
  305.       /* build the control file */
  306.       sprintf(file_buffer, "H%s\n",hostname);
  307.       sprintf(file_buffer+strlen(file_buffer), "P%s\n", user);
  308.       sprintf(file_buffer+strlen(file_buffer), "J%s\n", file_to_print);
  309.       sprintf(file_buffer+strlen(file_buffer), "C%s\n", hostname);
  310.       sprintf(file_buffer+strlen(file_buffer), "L%s\n", user);
  311.       sprintf(file_buffer+strlen(file_buffer), "fd%s\n", filename);
  312.       sprintf(file_buffer+strlen(file_buffer), "Ud%s\n", filename);
  313.       sprintf(file_buffer+strlen(file_buffer), "N%s\n", file_to_print);
  314.       sprintf(o_str, "\2%ld c%s\n", strlen(file_buffer), filename);
  315.       output(o_str);
  316.     if(get_response() > 1)
  317.     {
  318.        printf("%s",data);
  319.     }
  320.       if(response == '\1') 
  321.       {
  322.         printf("\r\n<\033pError: connection messed up, try again\033q>\r\n");
  323.         tcp_ok = FALSE;
  324.         return;
  325.       }
  326.       else if(response == '\2') 
  327.       {
  328.         printf("\r\n<\033pError: server out of storage space\033q>\r\n");
  329.         tcp_ok = FALSE;
  330.         return;
  331.       }
  332.       else if(response != '\0') 
  333.       {
  334.         tcp_ok = FALSE;
  335.         return;
  336.       }
  337.       output1((int)strlen(file_buffer)+1,file_buffer);
  338.     if(get_response() > 1)
  339.     {
  340.        printf("%s",data);
  341.     }
  342.       if(response != '\0') 
  343.       {
  344.         printf("\r\n<\033pError: control file not properly transferred, aborting\033q>\r\n");
  345.         tcp_ok = FALSE;
  346.         return;
  347.       }
  348. }
  349.  
  350. void output(char *o_str)
  351. {
  352.   int o_len;
  353.  
  354.     o_len = (int)strlen(o_str);
  355.     output1(o_len, o_str);
  356. }
  357.  
  358. void output1(int o_len, char *o_str)
  359. {
  360.   int i,j;
  361.  
  362.     for(i=0; i < o_len;)
  363.   {
  364.     j = (int)tcp_write(tcp,o_str+i,o_len-i,PUSH,NO_URGENT);
  365.     if(j>=0) i += j;
  366.     else
  367.     {
  368.       printf("connection broken\n");
  369.       tcp_ok = FALSE;
  370.       break;
  371.     }
  372. #ifdef DEBUG
  373. printf("send %d bytes\n",j);
  374. #endif
  375.   }
  376. }
  377.  
  378. int get_response(void)
  379. {
  380.   int length;
  381.  
  382.   do
  383.   {
  384.   length = (int)tcp_read(tcp,data,INBUFSIZE);
  385.   } while(!length);
  386.   if(length < 0) return length;
  387.   response = data[0];
  388.   return length;
  389. }
  390.  
  391. long set_pd(void)
  392. {
  393.   oldpd = SYSBASE->_run;
  394.   SYSBASE->_run = &_BasPag;
  395.   return 0;
  396. }
  397.  
  398. long restore_pd(void)
  399. {
  400.   SYSBASE->_run = oldpd;
  401.   return 0;
  402. }
  403.  
  404.